package edu.fiu.cis.personalize.server.clean;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public abstract class Normalizer
{
	private static List<Normalizer>	normalizers	= new ArrayList<Normalizer>();

	static {
		normalizers.add(new LowerCaseNormalizer());
		normalizers.add(new Canonicalizer());
		normalizers.add(new EndPeriodRemover());
	}

	public static List<Normalizer> cleaners() {
		return normalizers;
	}

	public static String runNormalizers(String word) {
		for (Normalizer c : normalizers) {
			word = c.clean(word);
		}
		return word;
	}

	public abstract String clean(String word);
}

class LowerCaseNormalizer extends Normalizer
{

	@Override
	public String clean(String word) {
		return word.trim().toLowerCase();
	}

}

class Canonicalizer extends Normalizer
{

	@Override
	public String clean(String word) {
		return java.text.Normalizer.normalize(word,
			java.text.Normalizer.Form.NFD).replaceAll(
			"\\p{InCombiningDiacriticalMarks}+", "");
	}

}

class EndPeriodRemover extends Normalizer
{
	private static HashSet<Character>	ignoreset	= new HashSet<Character>();
	static {
		ignoreset.add('-');
		ignoreset.add('.');
	}

	@Override
	public String clean(String w) {
		StringBuffer word = new StringBuffer(w);

		if (ignoreset.contains(w.charAt(0))) {
			word.deleteCharAt(0);
		}

		if (word.length() > 0
				&& ignoreset.contains(word.charAt(word.length() - 1))) {
			word.deleteCharAt(word.length() - 1);
		}
		return word.toString();
	}

}
